package org.example.day20240902;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int html_count = scanner.nextInt();
        int var_count = scanner.nextInt();
        scanner.nextLine();
        List<String> stringList = new ArrayList<>();
        for(int i = 0; i < html_count;i++){
            String line = scanner.nextLine();
            stringList.add(line);
        }
        Map<String,String> map= new LinkedHashMap<>();
        for(int i =0;i < var_count;i++){
            String line = scanner.nextLine();
//            name "David Beckham"
            int index = line.indexOf("\"");
            String key = line.substring(0,index-1);
            String value = line.substring(index+1,line.length()-1);
            map.put(key,value);
        }

        Pattern pattern = Pattern.compile("\\{\\{ (.*?) }}");
//        <html>
        for(String line : stringList){
            Matcher matcher = pattern.matcher(line);
//            <title>User {{ name }}</title>
            while (matcher.find()){
                String tmp = matcher.group();
                String var_name = matcher.group(1);
                line = line.replace(tmp,map.containsKey(var_name)?map.get(var_name):"");
            }
            System.out.println(line);
        }
    }
}
